package com.edu.exam.entity.organization;

import com.edu.exam.enums.organization.DepartmentStatus;
import com.edu.exam.entity.organization.DepartmentType;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.springframework.util.StringUtils;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 部门实体类
 * 所属模块：F-03 组织架构管理模块
 * 对应数据库表：department
 *
 * <p>管理部门的基本信息，包括部门编码、名称、类型、状态等。
 * 支持部门的创建、查询、更新和状态管理。
 * 关联上级部门、负责人、联系方式等关键信息，提供部门的完整视图。</p>
 *
 * @author System
 * @version 1.0.0
 */
@Entity
@EntityListeners(AuditingEntityListener.class)
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = false)
public class Department {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @NotBlank(message = "部门编码不能为空")
    @Size(max = 50, message = "部门编码长度不能超过50个字符")
    @Column(name = "code", unique = true, nullable = false, length = 50)
    private String code;

    @NotBlank(message = "部门名称不能为空")
    @Size(max = 100, message = "部门名称长度不能超过100个字符")
    @Column(name = "name", nullable = false, length = 100)
    private String name;

    @NotBlank(message = "部门简称不能为空")
    @Size(max = 50, message = "部门简称长度不能超过50个字符")
    @Column(name = "short_name", nullable = false, length = 50)
    private String shortName;

    @NotNull(message = "部门类型不能为空")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "type_id", nullable = false)
    private DepartmentType type;

    @Column(name = "description", columnDefinition = "TEXT")
    private String description;

    @Enumerated(EnumType.STRING)
    @Column(name = "status", nullable = false, length = 20)
    @Builder.Default
    private DepartmentStatus status = DepartmentStatus.NORMAL;

    @Column(name = "parent_id")
    private Long parentId;

    @Column(name = "level", nullable = false)
    @Builder.Default
    private Integer level = 1;

    @Column(name = "manager_id")
    private Long managerId;

    @Column(name = "display_order")
    private Integer displayOrder;

    @Column(name = "path", length = 500)
    private String path;

    @Column(name = "deleted", nullable = false)
    @Builder.Default
    private Boolean deleted = false;

    @CreatedDate
    @Column(name = "created_time", nullable = false, updatable = false)
    private LocalDateTime createdTime;

    @LastModifiedDate
    @Column(name = "updated_time", nullable = false)
    private LocalDateTime updatedTime;

    @Column(name = "created_by", length = 50)
    private String createdBy;

    @Column(name = "updated_by", length = 50)
    private String updatedBy;

    // 关联关系 - 部门的下级部门列表
    @OneToMany(mappedBy = "parent", fetch = FetchType.LAZY)
    @Builder.Default
    private List<Department> children = new ArrayList<>();

    // 关联关系 - 部门的上级部门
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "parent_id", insertable = false, updatable = false)
    private Department parent;


}